package com.zx.servicelearn.myaidl;

import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;

import com.zx.servicelearn.model.Book;

import java.util.List;


/**
 * Created by zhoux on 2018/3/2.
 * 注释：手写aidl接口实现
 */

public interface IBookManager2 extends IInterface {

    void add(Book book) throws RemoteException;

    List<Book> getList() throws RemoteException;

    public static abstract class BookManager2Stub extends Binder implements IBookManager2 {
        private static final java.lang.String DESCRIPTOR = "com.zx.servicelearn.myaidl.IBookManager2";//唯一标识
        static final int TRANSACTION_getList = (IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_add = (IBinder.FIRST_CALL_TRANSACTION + 1);


        public BookManager2Stub() {
            //
            this.attachInterface(this, DESCRIPTOR);
        }

        /**
         * 客户端将服务端的binder转为 接口类型对象
         *
         * @param obj
         * @return
         */
        public static IBookManager2 asInterface(IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            IInterface iin = obj.queryLocalInterface(DESCRIPTOR);//查询当前进程
            if (((iin != null) && (iin instanceof BookManager2Stub))) {
                return (BookManager2Stub) iin;
            }
            return new BookManager2Stub.BookManager2Stub.BookManager2Proxy(obj);

        }

        @Override
        public IBinder asBinder() {
            return this;
        }

        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {

            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_getList: {
                    data.enforceInterface(DESCRIPTOR);
                    List<Book> _result = this.getList();
                    reply.writeNoException();
                    reply.writeTypedList(_result);
                    return true;
                }
                case TRANSACTION_add: {
                    data.enforceInterface(DESCRIPTOR);
                    Book _arg0;
                    if ((0 != data.readInt())) {
                        _arg0 = Book.CREATOR.createFromParcel(data);
                    } else {
                        _arg0 = null;
                    }
                    this.add(_arg0);
                    reply.writeNoException();
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }

        private static class BookManager2Proxy implements IBookManager2 {
            private IBinder mRemote;

            public String getInterfaceDescriptor() {
                return DESCRIPTOR;
            }

            public BookManager2Proxy(IBinder obj) {
                this.mRemote = obj;
            }

            @Override
            public void add(Book book) throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((book != null)) {
                        _data.writeInt(1);
                        book.writeToParcel(_data, 0);
                    } else {
                        _data.writeInt(0);
                    }
                    mRemote.transact(TRANSACTION_add, _data, _reply, 0);
                    _reply.readException();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }

            @Override
            public List<Book> getList() throws RemoteException {
                Parcel _data = Parcel.obtain();
                Parcel _reply = Parcel.obtain();
                List<Book> _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    mRemote.transact(TRANSACTION_getList, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.createTypedArrayList(Book.CREATOR);
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            @Override
            public IBinder asBinder() {
                return mRemote;
            }
        }
    }

}
